package com.xh.ussines.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.ApplicationConfig;
import com.xh.bussiness.system.model.CourseSystem;
import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.system.service.ICourseSystemService;
import com.xh.bussiness.system.service.IDictionaryService;
import com.xh.bussiness.tiku.model.BookVersion;
import com.xh.bussiness.tiku.service.IBookVersionService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationConfig.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BookVersionInit {

    @Autowired
    private IDictionaryService dictionaryService;
    @Autowired
    private IBookVersionService bookVersionService;
    @Autowired
    private ICourseSystemService courseSystemService;

    @Test
    public void bookVersionInit() {
        List<CourseSystem> courseSystems = new ArrayList<>();
        QueryWrapper<Dictionary> qwd = new QueryWrapper<>();
        qwd.eq("type_code", "SUBJECT");
        List<Dictionary> dictionaries = dictionaryService.selectList(qwd, Dictionary.class);
        for (Dictionary dictionary : dictionaries) {
            String code = dictionary.getCode();
            String name = dictionary.getName();
            QueryWrapper<BookVersion> qwbv = new QueryWrapper<>();
            qwbv.eq("subject", code);
            qwbv.groupBy("version_id");
            List<BookVersion> bookVersions = bookVersionService.selectList(qwbv, BookVersion.class);
            for (BookVersion bookVersion : bookVersions) {
                QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("name", bookVersion.getVersionName());
                queryWrapper.eq("val", bookVersion.getVersionId());


                Dictionary dictionary1 = dictionaryService.selectOne(queryWrapper, Dictionary.class);

                String versionName = bookVersion.getVersionName();
                CourseSystem courseSystem = new CourseSystem();
                courseSystem.setSubjectId(code);
                courseSystem.setSubjectName(name);
                courseSystem.setRelationName(versionName);
                courseSystem.setRelationValue(dictionary1.getId());
                courseSystem.setRelationType("TEACH_BOOK_VERSION");
                courseSystems.add(courseSystem);
            }
        }
        boolean result = courseSystemService.batchInsert(courseSystems);
        System.out.println(result);
    }
}
